//
//  NewFeatureView.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/25.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
import SnapKit

private let  CZNewFeatureImage = 4
class NewFeatureView: UIView {


    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        
        setupImageUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var pageControl = UIPageControl()

}

extension NewFeatureView {

    private func setupImageUI(){
    
        //CGRectZero  ----->bounds
         let scollView = UIScrollView(frame:bounds)
         addSubview(scollView)
        
        for i in 0..<CZNewFeatureImage{
        
            let iv = UIImageView(image: UIImage(named:"new_feature_\(i+1)"))
            scollView.addSubview(iv)
            
            iv.frame = CGRectOffset(bounds, CGFloat(i) * bounds.width, 0)
           
        }
        
        //设置滚动的图像
        scollView.contentSize = CGSize(width: bounds.width * CGFloat(CZNewFeatureImage + 1), height: 0)
        
        //设置scollew的属性
        scollView.pagingEnabled = true
        scollView.bounces = false
        scollView.showsHorizontalScrollIndicator = false
        
        //添加分页空间
        addSubview(pageControl)
        
        //分页空间的属性
        pageControl.currentPage = 0
        pageControl.numberOfPages = CZNewFeatureImage
        pageControl.currentPageIndicatorTintColor = CZColor
        pageControl.pageIndicatorTintColor = UIColor.blackColor()
        pageControl.hidesForSinglePage =  true
        
        //自动布局
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-80)
        }
        //禁止跟用户交互
        pageControl.userInteractionEnabled = false
        
        //设置代理
        scollView.delegate = self
    }

}

extension NewFeatureView:UIScrollViewDelegate {

    
    //停止滚动的代理
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == CZNewFeatureImage {
        
        
        }
    }
    
    //滚动时调用该方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        pageControl.currentPage = page
        
        pageControl.hidden = (page == CZNewFeatureImage)
    }


}






